package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.EcFontRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.EcFont;
import com.ndp.fb.rdb.model.Pager;

import java.util.List;

/**
 * Created by infi.he on 2016/4/26.
 */
@Service
public class EcFontRdbServiceImpl extends DaoTemplate implements EcFontRdbService {
    @Override
    public EcFont findEcFontByFontNameAndfontWeight(Long orgId, String fontName, String fontWeight) {
        String sql = "select * from ec_font where 1=1 and org_id = ? and font_name = ? and font_weight = ?";
        return findOne(sql, new Object[]{orgId, fontName, fontWeight}, DefaultRowMapper.autoRowMapper(EcFont.class));
    }

    @Override
    public EcFont findByFontName(Long orgId, String fontName) {
        String sql = "select * from ec_font where 1=1 and org_id = ? and font_name = ?";
        return findOne(sql, new Object[]{orgId, fontName}, DefaultRowMapper.autoRowMapper(EcFont.class));
    }

    @Override
    public EcFont findByMD5(String md5) {
        String sql = "select * from ec_font where 1=1 and md5 = ? ";
        return findOne(sql, new Object[]{md5}, DefaultRowMapper.autoRowMapper(EcFont.class));
    }

    @Override
    public List<EcFont> findAll(Long orgId) {
        String sql = "select * from ec_font where is_delete = 0 and org_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{orgId}, DefaultRowMapper.autoRowMapper(EcFont.class));
    }

    @Override
    public Pager<EcFont> findPager(Long orgId, Integer cpage, Integer psize) {
        String sql = "select * from ec_font where is_delete = 0 and org_id = ? order by create_time desc";
        return findPager(sql, new Object[]{orgId}, cpage, psize, DefaultRowMapper.autoRowMapper(EcFont.class));
    }

    @Override
    public Long save(EcFont ecFont) {
        return saveResultId(ecFont);
    }

    @Override
    public List<EcFont> getByTemplateId(Long templateId) {
        String sql = "select DISTINCT e.* from ec_font e, ec_template_font f where e.id = f.font_id and e.is_delete = 0 and f.template_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{templateId}, DefaultRowMapper.autoRowMapper(EcFont.class));
    }
}
